package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Override
    public Map<String, Object> listInventory(Integer page,
                                             Integer rows,
                                             String codeOrName,
                                             Integer goodsTypeId) {
        //获取库存分页
        Map<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;

        List<Goods> goodsList = goodsDao.selectGoodsList(offSet, rows, codeOrName, goodsTypeId);
        map.put("total", String.valueOf(goodsList.size()));
        map.put("rows", goodsList);
        return map;
    }

    //添加修改商品
    @Override
    public ServiceVO saveOrUpdate(Goods goods, String goodsId) {
        //判断商品id是否为空 为空添加 不为空根据id修改
        if (goodsId == null) {
            this.goodsDao.saveGoods(goods);
        } else
            this.goodsDao.updateGoods(goods, goodsId);
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public ServiceVO deleteGoods(Integer goodsId) {
        //要判断商品状态,入库、有进货和销售单据的不能删除
        List<Goods> stateList = goodsDao.selectStateByGoodsId(goodsId);
        for (Goods goods : stateList) {
            if (goods.getState().intValue() != 1 && goods.getState().intValue() != 2) {
                this.goodsDao.deleteGoods(goodsId);
            }
        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public Map<String, Object> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        //获取库存分页
        Map<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;

        List<Goods> goodsList = goodsDao.selectNoInventory(offSet, rows, nameOrCode);
        //循环商品列表获取库存数比较库存为0的返回无库存状
        List<Goods> collect = goodsList.stream().filter(goods -> {
            return goods.getInventoryQuantity().intValue() == 0;
        }).collect(Collectors.toList());
        map.put("total", String.valueOf(goodsList.size()));
        map.put("rows", collect);

        return map;
    }

    @Override
    public Map<String, Object> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        //获取库存分页
        Map<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;

        List<Goods> goodsList = goodsDao.selectHasInventory(offSet, rows, nameOrCode);
        //判断有库存状态
        List<Goods> collect = goodsList.stream().filter(goods -> {
            return goods.getInventoryQuantity().intValue() != 0;
        }).collect(Collectors.toList());
        map.put("total", String.valueOf(goodsList.size()));
        map.put("rows", collect);
        return map;
    }

    @Override
    public ServiceVO saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        //添加库存、修改数量或成本价
        this.goodsDao.saveStock(goodsId, inventoryQuantity, purchasingPrice);
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public ServiceVO deleteStock(Integer goodsId) {
        //要判断商品状态,入库、有进货和销售单据的不能删除
        List<Goods> stateList = goodsDao.selectStateByGoodsId(goodsId);
        for (Goods goods : stateList) {
            if (goods.getState().intValue() != 1 && goods.getState().intValue() != 2) {
                this.goodsDao.deleteStock(goodsId);
            } else {
                return new ServiceVO(ErrorCode.STORED_ERROR_CODE, ErrorCode.HAS_FORM_ERROR_MESS);
            }
        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public Map<String, Object> listAlarm() {
        List<Goods> listAlarm = goodsDao.listAlarm();
        Map<String, Object> map = new HashMap<>();
        List<Goods> collect = listAlarm.stream().filter(goods -> {
            return goods.getInventoryQuantity().intValue() < goods.getMinNum().intValue();
        }).collect(Collectors.toList());
        map.put("rows", collect);
        return map;
    }
    //获取商品的编码
    @Override
    public ServiceVO getCode() {
        return null;
    }
}
